// Unit test for Council Room Card

#include "dominion.h"  
#include "dominion_helpers.h"  
#include "rngs.h"  
#include <stdio.h>  
#include <math.h>  
#include <stdlib.h>  
#include <assert.h>

#define MAX_CARD 26

int testCouncilRoom(int numPlayers, int * k, struct gameState *g){
    int i, j, player, topCard, card, retVal,  handCountBefore, handCountAfter, buyBefore, buyAfter;
    // choose random player
    player = floor(Random()*numPlayers);
    g->deckCount[player] = 5;
    // if hand is empty, draw 5 from deck
    if(g->handCount[player] == 0){
      for(i = 0 ; i < 5; i++){
        topCard = g->deckCount[player]-1;
        g->hand[player][i] = g->deck[player][topCard];
        g->deckCount[player]--;
        g->handCount[player]++;
      }
    }
    g->whoseTurn = player;
    handCountBefore = g->handCount[player];
    buyBefore = g->numBuys;
    // call function
    retVal = cardEffect(council_room, -1, -1, -1, g, 0, 0);
    handCountAfter = g->handCount[player];
    buyAfter = g->numBuys;
    // numBuys should increase by 1
    if(buyAfter != (buyBefore + 1))
      return 0;
    // make sure cardEffect returns correct value
    if(retVal != 0)
      return 0;
    // deck count increase by 3 since 4 cards are drawn and the smithy card is placed back in deck
    // fails when player != 0
    if(handCountAfter != (handCountBefore + 3))
      return 0;
    return 1;
}

int main(int argc, char *argv[]) {
  long seed;
  struct gameState g;
  int players, i, j, results, good, bad;
  int k[10] = {adventurer, gardens, great_hall, village, minion, mine, council_room, 
           baron, salvager, smithy};
  good = 0;
  bad = 0;
    printf("Card Test 4 -> Council Room Card :\n");
  if(argc < 2){
    printf("Usage: ./cardtest4.c [random_seed]\n");
  }
  for (j = 0; j < 1000; j++) {
    for (i = 0; i < sizeof(struct gameState); i++) {
      ((char*)&g)[i] = floor(Random() * 256);
    }
    // give different seed for every test 
    seed = (long)floor(Random()*(long)argv[1]+j);       
    players = j+2;
    // comment out next 2 lines to see bug
    if(players > 4)
      players = players%3+2; // makes sure num players = 2 to 4
    // had to set num players to 2 because anything else caused errors in functions other than the ones
    // I was testing
    initializeGame(2, k, seed, &g);
    results = testCouncilRoom(2, k, &g);
    if(results == 1){
    //  printf("Test Passed!\n");
      good++;
    }
    else{
    //  printf("Test Failed!\n");
      bad++;
    }
  //  printf("\n");
  }
  printf("Tests passed = %i\n", good);
  printf("Tests Failed = %i\n", bad);
  printf("TEST ALWAYS FAILS UNLESS PLAYER 0 IS RANDOMLY SELECTED AS THE CURRENT PLAYER\n");
  return 0;
}